/*
 * @Description:
 * @Author: 胡涛
 * @Date: 2023-09-26 09:04:12
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-11-30 23:29:48
 */
import { IComponentOptions } from "@/types";
import { Component, h, resolveComponent } from "vue";
import { toUpperCamelCaseName } from "./camel";
import {
  Input,
  InputGroup,
  InputNumber,
  Select,
  TreeSelect,
  DatePicker,
  MonthPicker,
  WeekPicker,
  TimePicker,
  RangePicker,
  Rate,
  Switch,
  Checkbox,
  Radio,
  RadioGroup,
  RadioButton,
  Cascader,
  AutoComplete,
  Slider,
  CheckboxGroup,
  FormInstance,
} from "ant-design-vue";
import EnumSelect from "@/components/enum-select/Index.vue";
import DictionarySelect from "@/components/dictionary-select/Index.vue";
import UserSelect from "@/components/user-select/Index.vue";
import RoleSelect from "@/components/role-select/Index.vue";
import RegionSelect from "@/components/region-select/Index.vue";
import EnteripseSelect from "@/components/enteripse-select/Index.vue";
import AdvancedSearchPanel from "@/components/advanced-search-panel/Index.vue";
import ProjectSelect from "@/components/project-select/Index.vue";
import FileIcon from "@/components/file-icon/Index.vue";
import ExtraPropteries from "@/components/extra-propteries/Index.vue";
import AttachmentInput from "@/components/attachment-input/Index.vue";
import ImagesInput from "@/components/images-input/Index.vue";
import MonacoEditor from "@/components/monaco-editor/Index.vue";

import { DefineComponent } from "vue";

const componets: Record<string, DefineComponent<any, any, any>> = {
  "a-input": Input,
  "a-inpujt-group": InputGroup,
  "a-input-number": InputNumber,
  "a-select": Select,
  "a-tree-select": TreeSelect,
  "a-date-picker": DatePicker,
  "a-month-picker": MonthPicker,
  "a-week-picker": WeekPicker,
  "a-time-picker": TimePicker,
  "a-range-picker": RangePicker,
  "a-rate": Rate,
  "a-switch": Switch,
  "a-checkbox": Checkbox,
  "a-checkbox-group": CheckboxGroup,
  "a-radio": Radio,
  "a-radio-group": RadioGroup,
  "a-radio-button": RadioButton,
  "a-cascader": Cascader,
  "a-auto-complete": AutoComplete,
  "a-slider": Slider,
  "enum-select": EnumSelect,
  "dictionary-select": DictionarySelect,
  "role-select": RoleSelect,
  "user-select": UserSelect,
  "region-select": RegionSelect,
  "enteripse-select": EnteripseSelect,
  "project-select": ProjectSelect,
  "monaco-editor": MonacoEditor,
  "attachment-input": AttachmentInput,
  "images-input": ImagesInput,
};

/**
 * 创建组件对象
 * @param name
 * @param props
 * @param content
 */
export const createComponent = (options: IComponentOptions) => {
  let children: Array<any> | undefined = undefined;
  if (options.content) {
    children = [options.content];
  } else if (options.children) {
    children = options.children.map((x) => createComponent(x));
  }
  let component = getComponent(options.name);
  if (component) {
    return h(component, options.props, children);
  } else {
    return h(resolveComponent(options.name), options.props, children);
  }
};

/**
 * 获取组件对象
 * @param name
 * @returns
 */
export const getComponent = (name: string) => {
  let comName = toUpperCamelCaseName(name);
  return resolveComponent(comName);
};

/**
 * 获取组件类型
 * @param name
 */
export const getComponentType = (name: string) => {
  return componets[name] || name ? name : Input;
};
